package com.itheima.map;

import java.util.HashMap;

/**
 * @Author 王加鑫
 * @Date 2021/5/16 18:27
 * @Version 1.0
 */
public class MapDemo05 {
    public static void main(String[] args) {
        String s = "aababcabcdabcde";
        HashMap<Character,Integer> map = new HashMap<>();
        //遍历字符串取出每一位字符
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //当map集合中不存在该key时,即给该字符的次数赋值为1
            if(!map.containsKey(c)){
                map.put(c,1);
            }else {
                //map集合根据键获取值
                int count = map.get(c);
                //如果k在集合中重复,那么value值+1
                count++;
                map.put(c,count);
            }
        }

        map.forEach((k,v) -> System.out.print(k  + "(" + v + ") " ));


    }
}
